package com.eastcom.mystope.commandline;

import java.util.ArrayList;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.mina.transport.socket.nio.NioSocketConnector;

import com.eastcom.mystope.common.util.SpringContextProvider;
import com.eastcom.mystope.daemon.config.GollfPropertyPlaceholderConfigurer;
import com.eastcom.mystope.daemon.config.ServerPath;
import com.eastcom.mystope.daemon.context.ServerContext;
import com.eastcom.mystope.daemon.core.app.Application;
import com.eastcom.mystope.daemon.tools.JaxbUtil;


public class Launcher extends Application{
	private static Log logger = LogFactory.getLog(Launcher.class);
	private NioSocketConnector socketConnector;
	public static void main(String[] args) {
		if(args.length == 2){
			Launcher app = new Launcher();
			app.setArgs(args);
			app.run();
			System.exit(1);
		}
	}

	@Override
	protected boolean preInitialize() {
		ServerPath serverPath = JaxbUtil.converyFileToJavaBean(args[1], "UTF-8",ServerPath.class);
		ServerContext.serverPath = serverPath;
		if(serverPath.getBaseConfigPath() == null){
			logger.error("服务初始化文件路径不对。");
			return false;
		}
		return true;
	}

	@Override
	protected void printUsage() {
		// TODO 自动生成的方法存根

	}

	@Override
	protected void mainloop() {
		// TODO 自动生成的方法存根
		
		socketConnector = (NioSocketConnector) SpringContextProvider.getBean("commandClientIoConnector");
		SocketClient socketClient = SpringContextProvider.getBean("socketClient");
		socketClient.setConnector(socketConnector);
		socketClient.start();
		try {
			socketClient.join();
		} catch (InterruptedException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
	}

	@Override
	protected ArrayList displayStatus(String command) {
		// TODO 自动生成的方法存根
		return null;
	}

	public NioSocketConnector getSocketConnector() {
		return socketConnector;
	}

	public void setSocketConnector(NioSocketConnector socketConnector) {
		this.socketConnector = socketConnector;
	}
}
